<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars
define("PRIMARY_KEY","jo_id");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_transaction_jolist.php";
$FileTemplate  = "ais_transaction_jolist_fromtruck_list.html";
$FormName            = GetParam("FormName","");


$SubHeaderTitle = "Job Order Lookup";
$PageSize      = 10 ;
$HTMLGridList  = "JOGrid";
//************ FORM ACTION **************
    if (GetParam("Action","")=="Delete") {
        DeleteRecord ("txtCheck",$PageSize);
    }

//Initiate Database Table For Search;
$FNama = GetParam("staffSearch","");
$JOCode = GetParam("JOCodeSearch","");
$FDept = GetParam("deptSearch","");
$need_another_var = GetParam("need_another_var","");

//******* DATABASE FORM QUERY ***********
//$SQL = "SELECT *,trim(nik) as nik, tbldept.deptnam as deptnam FROM $DBGeneral.tblstaff LEFT JOIN tbldept ON $DBGeneral.tblstaff.kddept=tbldept.kddept ";
//$SQLCount = "SELECT count(*) FROM $DBGeneral.tblstaff ";

/*$SQL = "SELECT * FROM tbljoborder a
        JOIN tblproject b ON a.project_id = b.project_id
        JOIN ".$DBGeneral.".tblstaff d ON a.staff_id = d.staff_id
        JOIN tbldebtur c ON a.debtur = c.CUCODE ";*/

$SQL = "SELECT * FROM `tbljoborder` as a ";
$SQL.= "JOIN tbljoactivity as e ON a.jo_id = e.jo_id ";
$SQL.= "JOIN tblproject as b ON a.project_id = b.project_id ";
$SQL.= "JOIN ".$DBGeneral.".tblstaff as c ON a.staff_id = c.staff_id ";
$SQL.= "JOIN tbldebtur as d ON a.debtur = d.CUCODE ";


$SQLCount = "SELECT count(*) FROM tbljoborder a ";
$SQLCount.= "JOIN tbljoactivity as e ON a.jo_id = e.jo_id ";
$SQLCount.= "JOIN tblproject as b ON a.project_id = b.project_id ";
$SQLCount.= "JOIN ".$DBGeneral.".tblstaff as c ON a.staff_id = c.staff_id ";
$SQLCount.= "JOIN tbldebtur as d ON a.debtur = d.CUCODE ";

/*if ($FNama)
    if (strlen($WHERE)) $WHERE .= " AND "." nama like '".$FNama."%'";
    else $WHERE .= " nama like '".$FNama."%'";
if ($FCode)
    if (strlen($WHERE)) $WHERE .= " AND "." nik like '".$FCode."%'";
    else $WHERE .= " nik like '".$FCode."%'";

    if (strlen($WHERE)) $WHERE = " WHERE ".$WHERE;
    else $WHERE = " WHERE 1";*/

/*$SQL = $SQL.$WHERE." ORDER BY jo_code";
$SQLCount = $SQLCount.$WHERE." ORDER BY jo_code";*/

if ($JOCode)
    if (strlen($WHERE)) $WHERE .= " AND "." a.jo_code like '%".$JOCode."%'";
    else $WHERE .= " a.jo_code like '%".$JOCode."%'";

if ($projName)
    if (strlen($WHERE)) $WHERE .= " AND "." b.project_name like '%".$projName."%'";
    else $WHERE .= " b.project_name like '%".$projName."%'";

if (strlen($WHERE)) $WHERE = " WHERE ".$WHERE;
    //else $WHERE = " WHERE 1 ";
    //else $WHERE = " WHERE jo_appv_status='approved' ";
    else $WHERE = " WHERE jo_appv_status='approved' AND e.services_id = 1 ";

$SQL = $SQL.$WHERE." GROUP BY a.jo_id ORDER BY a.jo_id DESC";
$SQLCount = $SQLCount.$WHERE." GROUP BY a.jo_id ORDER BY a.jo_id";

//print $SQL;
//Select Department
$SQLDept = "SELECT * FROM tbldept";

//******* SHOW FORM CONTENT *********
$PageNum = 1;
$RecCnt = $DBConnection->dbc->get_var($SQLCount) ;

if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);
$TBS->MergeBlock("blg",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
$TBS->MergeBlock("deptblk",$DBMysql,$SQLDept);
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize) ;
$TBS->Show();


function DeleteRecord ($sequence_name,$PageSize) {
    global $DBConnection,$FileName;
    for ($x=1;$x<=$PageSize;$x++) {
        if (GetParam($sequence_name.$x,"")) {
            $DBConnection->Delete("tblstaff",$sequence_name.$x);
        }
    }
    header("Location: $FileName");
    exit;

}

?>
